package com.example.service.impl;

import com.example.domain.User;
import com.example.domain.UserLeaveDTO;
import com.example.exception.InvalidPasswordException;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author 符整国
 * @version 1.0
 */
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;


    @Override
    @Transactional
    public void changePassword(String username, String oldPassword, String newPassword) {
        //根据用户名查询用户
        User user = userMapper.getByUsername(username);
        //判断用户是否存在
        if (user == null){
            throw new InvalidPasswordException("用户不存在");
        }
        //判断旧密码是否正确
        if (!user.getPassword().equals(oldPassword)){
            throw new InvalidPasswordException("旧密码错误");
        }
        //判断无误，更新密码
        userMapper.updatePasswordByUsername(username,newPassword);
    }

    /**
     * 请假
     * @param userLeaveDTO
     */
    @Override
    public void leave(UserLeaveDTO userLeaveDTO) {
        //获取用户信息
        User user = userMapper.getById(userLeaveDTO.getId());
        //判断用户是否存在
        if (user == null){
            throw new InvalidPasswordException("用户不存在");
        }
        userLeaveDTO.setLeaveState(0);//设置审核状态默认为未审核
        //添加请假信息
        userMapper.insertLeave(userLeaveDTO);

    }

}
